﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;

namespace School.Pages.Evaluation
{
    public partial class students_evaluations : System.Web.UI.Page
    {
        public static SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
           if(IsPostBack)
            fillgridview();
        }

        private void fillgridview()
        {


            Int64 x_class = Int64.Parse(((DropDownList)evaluation_student1.FindControl("ddlclass")).SelectedValue);
            Int64 x_evaluation_type = Int64.Parse(((DropDownList)evaluation_student1.FindControl("ddlevaluation_type")).SelectedValue);
            if (x_class != 0 && x_evaluation_type != 0)
            {
                long[] q = new long[2000];
                q = (from r in db.TE_Class_Student
                     where r.Class_Id == x_class
                     select r.Student_Id).ToArray();
                for (int i = 0; i < q.Count(); i++)
                {
                    long x = q[i];
                    var q1 = (from r in db.TE_Evaluation_Result
                              where r.Student_Id == x
                              select r.Student_Id).ToList();
                    if (q1.Count == 0)
                    {
                        TE_Evaluation_Result re = new TE_Evaluation_Result() { Student_Id = q[i], Evaluation_Id = int.Parse(x_evaluation_type.ToString())  };
                        db.AddToTE_Evaluation_Result(re);
                        db.SaveChanges();

                    }



                }
                var sourcee = from r in db.TE_Evaluation_Result
                              where r.Evaluation_Id == x_evaluation_type
                              select new { name = r.TE_Student.Ar_FirstName + " " + r.TE_Student.Ar_SecondName + " " + r.TE_Student.Ar_ThirdNmae + " " + r.TE_Student.Ar_FourthName, id = r.Id , degree=r.Degree};

                GridView1.DataSource = sourcee;
               
                GridView1.DataBind();
                
            }
            }

        

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            fillgridview();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            fillgridview();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            float degree = Convert.ToSingle(((TextBox)GridView1.Rows[e.RowIndex].FindControl("t_degree")).Text);
            int id = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].FindControl("l_id")).Text);
            var q1 = (from r in db.TE_Evaluation_Result
                      where r.Id == id
                      select r).First();
            q1.Degree = degree;
            db.SaveChanges();
            GridView1.EditIndex = -1;
            fillgridview();

        }

      
    }
}